const app = getApp();
import dayjs from "../../../../utils/dayjs";
const kugou = require("../../../../utils/kugouMusic");
const backgroundAudioManager = wx.getBackgroundAudioManager();
// packageMore/pages/music/songList/songList.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hide: app.globalData.hide,
    flag: false,
    myInterval: null,
    songList: [],
    banner: "",
    listType: "",
    listId: "",
    listPage: 1,
    noMore: false,
    currentHash: app.globalData.currentHash || "",
    singerInfo: {},
    introMore: false,
    isPlay: app.globalData.isPlay || false
  },

  getSongList(id, type, page = 1) {
    const that = this;
    switch (type) {
      case "rank":
        kugou
          .rankMusicList({
            rankid: id,
            page,
            json: true,
          })
          .then((res) => {
            let banner = "";
            let songList = res.data.songs.list;
            let bannerurl = res.data.info.bannerurl;
            let banner_9 = res.data.info.banner_9;
            if (bannerurl) {
              banner = bannerurl.replace("{size}/", "");
            } else {
              banner = banner_9.replace("{size}/", "");
            }
            let _songList = [...that.data.songList, ...songList];
            if (res.data.songs.total == 0) {
              that.setData({
                noMore: true,
              });
            }
            that.setData({
              // songList: res.data.songs.list,
              songList: _songList,
              banner,
              listType: type,
              listId: id,
            });
          })
          .catch((err) => {
            wx.showToast({
              title: err.errMsg,
              icon: "none",
            });
          });
        break;
      case "plist":
        kugou
          .plistMusicList(id)
          .then((res) => {
            let singerInfo = res.data.info.list;
            let songList = res.data.list.list.info;
            that.setData({
              singerInfo,
              songList,
              listType: type,
              listId: id,
            });
          })
          .catch((err) => {
            wx.showToast({
              title: err.errMsg,
              icon: "none",
            });
          });
        break;

      default:
        break;
    }
  },

  playMusic(e) {
    const that = this;
    const { hash } = e.currentTarget.dataset;
    const { isPlay, currentHash } = that.data;
    if (isPlay && currentHash == hash) {
      app.handleMusic(true, () => {
        that.setData({ isPlay: false });
      });
      return;
    }
    if (!isPlay && currentHash == hash) {
      app.handleMusic(false, () => {
        that.setData({ isPlay: true });
      });
      return;
    }
    kugou.infoMusic(hash).then((result) => {
      // console.log(result.data);
      let { error, singerName, songName, album_img, url } = result.data;
      album_img = album_img.replace('{size}/', '');
      if (error != "") {
        wx.showToast({
          title: error,
          icon: "none",
        });
      } else {
        that.setData({
          currentHash: hash,
          isPlay: true
        });
        app.globalData.isPlay = true;
        app.globalData.currentHash = hash;

        app.setMisic(
          { singerName, songName, album_img, url },
          () => {
            app.globalData.isPlay = false;
            app.globalData.currentHash = '';
            that.setData({ currentHash: '', isPlay: false });
          },
          () => {
            app.globalData.isPlay = true;
            that.setData({ isPlay: true });
          },
          () => {
            app.globalData.isPlay = false;
            that.setData({ isPlay: false });
          }
        );
        // that.setMisic({ singerName, songName, imgUrl, url });
      }
    })
      .catch((err) => {
        wx.showToast({
          title: err.errMsg,
          icon: "none",
        });
      });
  },

  goAuthorInfo(e) {
    const { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: "/packageMore/pages/music/singer/info/info?id=" + id,
    });
  },

  handleIntro() {
    this.setData({
      introMore: true,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    const { id, type } = options;
    this.setData({
      songList: [],
    });
    this.getSongList(id, type);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      currentHash: app.globalData.currentHash || "",
      isPlay: app.globalData.isPlay || false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let { listType, listId, listPage, noMore } = this.data;
    if (listType == 'plist') return;
    if (noMore) {
      wx.showToast({
        title: "没有更多了",
        icon: "none",
      });
    } else {
      listPage++;
      this.setData({
        listPage,
      });
      this.getSongList(listId, listType, listPage);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
